package com.tdam.CommunicationManager;

//import android.app.Activity;
import com.tdam.CommunicationManager.MessagesService.Notificable;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.Toast;

public class Initial extends TabActivity implements Notificable {
    /** Called when the activity is first created. */
	private Menu menu;
	private MessagesService _service;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Contacts.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("contacts").setIndicator(res.getString(R.string.contacts),
                          res.getDrawable(R.drawable.contacts))
                          .setContent(intent);
        tabHost.addTab(spec);

        // Initialize a TabSpec for each tab and add it to the TabHost
        intent = new Intent().setClass(this, WebMail.class);
        spec = tabHost.newTabSpec("webMail").setIndicator(res.getString(R.string.webMail),
                          res.getDrawable(R.drawable.web_mail))
                          .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, History.class);
        spec = tabHost.newTabSpec("historial").setIndicator(res.getString(R.string.history),
                          res.getDrawable(R.drawable.historial))
                          .setContent(intent);
        tabHost.addTab(spec);        
        
        intent = new Intent().setClass(this, MyInfo.class);
        spec = tabHost.newTabSpec("myInfo").setIndicator(res.getString(R.string.myInfo),
                          res.getDrawable(R.drawable.perfil))
                          .setContent(intent);
        tabHost.addTab(spec);        
        
        tabHost.setCurrentTab(1);        
        startService(new Intent(Initial.this, MessagesService.class));
    }

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	super.onConfigurationChanged(newConfig);
	}    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	this.menu=menu;
        
    	//Inflate the currently selected menu XML resource.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.navigation_menu, menu);    	
    	return true;
    	
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        menu.close();
    	switch (item.getItemId()) {
            // For "Title only": Examples of matching an ID with one assigned in
            //                   the XML
            case R.id.Contacts:
                //Toast.makeText(this, "Contacts opens", Toast.LENGTH_SHORT).show();
        		Intent intent = new Intent(this, Contacts.class);
        		startActivity(intent);
                return true;

            case R.id.History:
        		Intent intent2 = new Intent(this, History.class);
        		startActivity(intent2);
                return true;

            case R.id.MyInfo:
                Toast.makeText(this, "my info opens", Toast.LENGTH_SHORT).show();
                return true;

            case R.id.WebMail:
                Toast.makeText(this, "webMail opens", Toast.LENGTH_SHORT).show();
                return true;              
                
           
                
            // Generic catch all for all the other menu resources
            default:
                //Don't toast text when a submenu is clicked
                if (!item.hasSubMenu()) {
                    Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                break;
        }
        
        return false;
    }    
    
    
    
    public boolean onPrepareOptionsMenu(Menu navigationMenu) {
    	navigationMenu.clear();
    	MenuInflater inflater = new MenuInflater(this);
    	inflater.inflate(R.menu.navigation_menu, navigationMenu);
    	return true;
    	}

	@Override
	public boolean notificar(int acccion, Bundle datos) {
		// TODO Auto-generated method stub
		return false;
	}    
	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			_service = ((MessagesService.LocalBinder) service).getService();
			_service.bind(Initial.this);
		}

		public void onServiceDisconnected(ComponentName className) {
			_service = null;
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		bindService(new Intent(Initial.this, MessagesService.class), mConnection,
				0);
	}

	@Override
	protected void onPause() {
		super.onPause();
		_service.unBind(this);
		unbindService(mConnection);
	}
}